package com.ruoyi.web.controller.main;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.InsuredPersons;
import com.ruoyi.system.domain.Medicalunit;
import com.ruoyi.system.domain.ReimbursementRecords;
import com.ruoyi.system.domain.TransactionRecord;
import com.ruoyi.system.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * @description: 定点医疗医院
 * @author：tuimao
 * @date: 2025/4/16
 */
@RestController
@RequestMapping("/system/medicalunit")
public class MedicalunitController {

    @Autowired
    private IReimbursementApplicationService reimbursementApplicationService;

    @Autowired
    private IInsuredPersonsService insuredPersonsService;

    @Autowired
    private IFinanceDepartInfoService financeDepartInfoService;

    @Autowired
    private ITransactionRecordService transactionRecordService;

    @Autowired
    private IReimbursementRecordsService reimbursementRecordsService;

    @ApiOperation("定点医疗医院统计信息")
    @GetMapping(value = "/info}")
    public AjaxResult getInfo()
    {
        Medicalunit medicalunit = new Medicalunit();
        List<ReimbursementRecords> list = reimbursementRecordsService.selectReimbursementRecordsList(new ReimbursementRecords());
        if(CollectionUtils.isNotEmpty(list)){
            list = list.stream().map(item -> {
                InsuredPersons insuredPersons = insuredPersonsService.selectInsuredPersonsById(item.getInsuredPersonD());
                if(ObjectUtils.isNotEmpty(insuredPersons)){
                    item.setInsuredPersons(insuredPersons);
                }
                return item;
            }).collect(Collectors.toList());

            medicalunit.setRecordsList(list);


            BigDecimal total = list.stream()
                    .map(ReimbursementRecords::getReimbursementAmount) // 提取字段
                    .filter(Objects::nonNull) // 过滤掉 null 值
                    .reduce(BigDecimal.ZERO, BigDecimal::add); // 累加

            medicalunit.setAllMoney(total);

        }

        List<TransactionRecord> transactionRecords = transactionRecordService.selectTransactionRecordList(new TransactionRecord());
        if(CollectionUtils.isNotEmpty(transactionRecords)){
            List<Long> collect = transactionRecords.stream().map(TransactionRecord::getUserId).filter(Objects::isNull).distinct().collect(Collectors.toList());
            medicalunit.setPersonNumber(collect.size());
        }
        return success(medicalunit);
    }
}
